home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5318 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: ix.netcom.com!netnews
  2. From: judgemi@ix.netcom.com (Michael Judge )
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: error checking for int
  5. Date: 3 Feb 1996 18:30:14 GMT
  6. Organization: Netcom
  7. Message-ID: <4f09jm$ob3@reader2.ix.netcom.com>
  8. References: <Pine.SUN.3.91.960203120230.12797A-100000@chip.bgsu.edu>
  9. NNTP-Posting-Host: bos-ma10-09.ix.netcom.com
  10. X-NETCOM-Date: Sat Feb 03 10:30:14 AM PST 1996
  11.  
  12. In <Pine.SUN.3.91.960203120230.12797A-100000@chip.bgsu.edu> Andrew
  13. Turner <turner@chip.bgsu.edu> writes: 
  14. >
  15. >
  16. >  I have an array of ints declared
  17. >    
  18. >    int something[20];
  19. >and when I pass it a char it goes into an endless loop, ex.
  20. >
  21. >    cout>>"\nEnter a number from 1-20: ";
  22. >    cin<<something[0];
  23. >
  24.  
  25. First problem is that you have the ">>" and "<<" backward in a few
  26. places, I assume they are typos.
  27. I tried this in Visual C++ in a quickwin application and it produces a
  28. 0 for something[0]. If that is not the case then you have a problem in
  29. your iostream library.
  30.  
  31. >if the person enters a char then the program goes bonkers.  One 
  32. >modification that I have tried is
  33. >
  34. >    cout>>"\nEnter a number from 1-20: ";
  35. >    cin<<temp;    //where temp is declared as an int.
  36. >    while ((temp<1) || (temp >20))
  37. >    {
  38. >       temp=0;
  39. >       cout>>"\nEnter a valid number from 1-20: ";
  40. >       cin>>temp;
  41. >    }
  42. >    something[0]=temp;
  43. >
  44. >but all this seems to do is go into a loop that prints 
  45. >
  46. >    Enter a valid number from 1-20:
  47. >
  48. >to the screen a million times or until I break out of it.
  49.  
  50.  
  51. solve this with a sync_with_stdio. it is a method of ios.
  52.  
  53.     int temp;
  54.     int something[20];
  55.     cout<<"\nEnter a number from 1-20: ";
  56.     cin>>temp;    //where temp is declared as an int.
  57.     while ((temp<1) || (temp >20))
  58.     {
  59. //next line is key
  60.        cin.sync_with_stdio();
  61.        temp=0;
  62.        cout<<"\nEnter a valid number from 1-20: ";
  63.        cin>>temp;
  64.     }
  65.     something[0]=temp;
  66.  
  67. MJ
  68.  
  69.